package com.cx.service.impl;

import com.cx.common.util.CamelCaseUtils;
import com.cx.common.util.StringUtils;
import com.cx.dal.dao.CxMusicMapper;
import com.cx.meta.dto.CxMusic;
import com.cx.meta.model.po.MusicQueryParams;
import com.cx.meta.model.vo.PageData;
import com.cx.service.MusicService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author flysand
 * @package com.cx.service.impl
 * @date 2021-11-16 22:46
 * @des MusicServiceImpl:
 */
@Service
public class MusicServiceImpl extends CommonServiceImpl implements MusicService  {

    @Resource
    private CxMusicMapper musicMapper;

    @Override
    public PageData<CxMusic> getMusicByParams(MusicQueryParams queryParams) {
        // 分页查询
        int page = queryParams.getPage() == 0 ? 1 : queryParams.getPage();
        int rows = queryParams.getRows() == 0 ? 10 : queryParams.getRows();
        String orderBy = "";
        String sort = queryParams.getSort();
        if (StringUtils.isNotBlank(sort)) {
            sort = CamelCaseUtils.humpToLine(sort);
            orderBy = sort + " desc";
        }
        if (StringUtils.isNotBlank(queryParams.getOrder())) {
            orderBy = orderBy.replace("desc", queryParams.getOrder());
        }

        if (StringUtils.isBlank(orderBy)) {
            PageHelper.startPage(page, rows);
        }else {
            PageHelper.startPage(page, rows, orderBy);
        }

        Page<CxMusic> pageInfo = musicMapper.getByParams(queryParams);

        PageData<CxMusic> pageData = new PageData<>();
        pageData.setRows(pageInfo.getResult());
        pageData.setTotal((int) pageInfo.getTotal());
        return pageData;
    }
}
